package com.woniuxy.order.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.order.client.HouseService;
import com.woniuxy.order.entity.RenthouseOrder;
import com.woniuxy.order.entity.ResponseResult;
import com.woniuxy.order.entity.SecondhouseOrder;
import com.woniuxy.order.service.SecondhouseOrderService;
import com.woniuxy.order.util.OrderNumberCreator;
import io.swagger.annotations.ApiResponse;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maotao
 * @since 2021-03-19
 */
@Controller
@RequestMapping("/secondhouseOrder")
public class SecondhouseOrderController {
    @Autowired
    SecondhouseOrderService secondhouseOrderService;
    @Autowired
    HouseService houseService;
    @Autowired
    AmqpTemplate amqpTemplate;
    @PostMapping("")
    public ResponseResult add(SecondhouseOrder order){
        amqpTemplate.convertAndSend("topicExchange","topic.second",order);
        return ResponseResult.SUCCESS;
    }
    @ApiResponse(message = "增加订单接口", code = 301)
    @PostMapping("add")
    public ResponseResult add(@Valid RenthouseOrder order) {
        order.setTime(LocalDateTime.now());
        order.setOrderno(OrderNumberCreator.createServerOrderNum());
        amqpTemplate.convertAndSend("topicExchange", "topic.rent", order);
        return ResponseResult.SUCCESS;
    }
    @GetMapping("/findByPage/{currentPage}/{size}")
//    @SentinelResource(value = "findByPage",blockHandler = "sentinelTestHandler",fallback = "findByPageTestFallBack")
    public ResponseResult findByPage(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {
        Page<SecondhouseOrder> pageResult = secondhouseOrderService.page(new Page<>(currentPage, size));
        ResponseResult responseResult = new ResponseResult<>();
        responseResult.setData(pageResult);
        return responseResult;
    }
    @GetMapping("findById/{id}")
    public ResponseResult findById(@PathVariable("id") Integer id) {
        SecondhouseOrder secondhouseOrder = secondhouseOrderService.getById(id);
        Integer houseid = secondhouseOrder.getHouseid();
        ResponseResult result = houseService.findSecondHouseById(id);
        return result;
    }
}

